package com.mr.qa.bo;

import java.util.List;
import java.util.Set;

import com.mr.qa.dao.impl.ShopArticleDAO;

public class ShopArticle extends BaseBO {
	private String picture;
	private String name;
	private String description;
	private Integer money;
	private Integer quantity;
	private ShopCategory category;
	private User awardUser;
	private Integer awardMoney;
	private Set history;
	private Integer exchangeCount;
	
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Integer getMoney() {
		return money;
	}
	public void setMoney(Integer money) {
		this.money = money;
	}
	public Integer getQuantity() {
		return quantity;
	}
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}
	public ShopCategory getCategory() {
		return category;
	}
	public void setCategory(ShopCategory category) {
		this.category = category;
	}
	public Set getHistory() {
		return history;
	}
	public void setHistory(Set history) {
		this.history = history;
	}
	public User getAwardUser() {
		return awardUser;
	}
	public void setAwardUser(User awardUser) {
		this.awardUser = awardUser;
	}
	public Integer getAwardMoney() {
		return awardMoney;
	}
	public void setAwardMoney(Integer awardMoney) {
		this.awardMoney = awardMoney;
	}
	public Integer getExchangeCount() {
		return exchangeCount;
	}
	public void setExchangeCount(Integer exchangeCount) {
		this.exchangeCount = exchangeCount;
	}
	public void add(){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		shopArticleDAO.save(this);
	}
	
	public void update(){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		shopArticleDAO.modify(this);
	}
	
	public ShopArticle getByName(String name){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		return shopArticleDAO.findByName(name);
	}
	
	public ShopArticle getByNameExceptThis(String name, Integer id){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		return shopArticleDAO.findByNameExceptId(name, id);
	}
	
	public ShopArticle getById(Integer id){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		return shopArticleDAO.findById(id);
	}
	
	public void delete(){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		shopArticleDAO.delete(this);
	}
	
	
	public List getAllArticles(Integer categoryId, int page, int pageSize){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		String whereClause = null;
		if(categoryId != null && categoryId > 0){
			whereClause = "where category.id=" + categoryId;
		}
		shopArticleDAO.setPage(page, pageSize);
		return shopArticleDAO.getArticles(whereClause, "order by money");
	}
	
	public Integer getAllArticlesCount(Integer categoryId, int page, int pageSize){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		String whereClause = null;
		if(categoryId != null && categoryId > 0){
			whereClause = "where category.id=" + categoryId;
		}
		shopArticleDAO.setPage(page, pageSize);
		return shopArticleDAO.getArticlesCount(whereClause, "order by money");
	}
	
	
	public List getAllArticlesNoZero(Integer categoryId, int page, int pageSize){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		String whereClause = "where quantity>0";
		if(categoryId != null && categoryId > 0){
			whereClause += " and category.id=" + categoryId;
		}
		shopArticleDAO.setPage(page, pageSize);
		return shopArticleDAO.getArticles(whereClause, "order by money");
	}
	
	public Integer getAllArticlesNoZeroCount(Integer categoryId, int page, int pageSize){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		String whereClause = "where quantity>0";
		if(categoryId != null && categoryId > 0){
			whereClause += " and category.id=" + categoryId;
		}
		shopArticleDAO.setPage(page, pageSize);
		return shopArticleDAO.getArticlesCount(whereClause, "order by money");
	}
	
	public List getAllArticlesNoZeroOrderByExchange(int page, int pageSize){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		String whereClause = "where quantity>0";
		shopArticleDAO.setPage(page, pageSize);
		return shopArticleDAO.getArticles(whereClause, "order by exchangeCount desc");
	}
	
	public List getAllArticlesNoZeroOrderByExchangeNoPagination(){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		String whereClause = "where quantity>0";
//		shopArticleDAO.setPage(page, pageSize);
		return shopArticleDAO.getArticles(whereClause, "order by exchangeCount desc");
	}
	
	public List getAllArticlesExchangeList(Integer categoryId, int page, int pageSize){
		ShopArticleDAO shopArticleDAO = new ShopArticleDAO();
		shopArticleDAO.setPage(page, pageSize);
		return shopArticleDAO.getAllArticlesExchangeList(categoryId);
	}
	
}
